/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.table;

import com.jspx.json.NotExpose;
import com.jspx.sober.annotation.*;
import com.jspx.sober.table.OperateTable;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.StringUtil;
import java.util.Date;


/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-3-10
 * Time: 9:12:29
 * 数结构
 * com.jspx.txweb.table.TreeItem
 */

@Table(name = "jspx_tree_item", caption = "栏目树", cache = true, cacheName = "treeItemCache")
public class TreeItem extends OperateTable {
    public TreeItem() {

    }

    @Id(auto = true, type = IDType.serial)
    @Column(caption = "ID", notNull = true)
    private long id = 0;

    @Column(caption = "节点ID", length = 50, notNull = true)
    private String nodeId = StringUtil.empty;

    @Column(caption = "节点值", length = 200, notNull = true)
    private String itemValue = StringUtil.empty;

    @Column(caption = "父ID", length = 100, notNull = true)
    private String parentNodeId = StringUtil.empty;

    @Column(caption = "名称", length = 100, notNull = true)
    private String caption = StringUtil.empty;

    @NotExpose
    @Column(caption = "拼音", length = 100, hidden = true, notNull = true)
    private String spelling = StringUtil.empty;

    @Column(caption = "节点动作", length = 100, notNull = true)
    private String nodeAction = StringUtil.empty;

    @Column(caption = "图标", length = 100, notNull = true)
    private String icon = StringUtil.empty;

    @Column(caption = "打开的图标", length = 100, notNull = true)
    private String openIcon = StringUtil.empty;

    @Column(caption = "隐藏", option = "0:不隐藏;1:隐藏", notNull=true, defaultValue = "0")
    private int hide = 0;

    @Column(caption = "录入框", length = 50)
    private String inputType = StringUtil.empty;

    @Column(caption = "排序", notNull = true, defaultValue = "0")
    private int sortType = 0;

    @Column(caption = "页面排序", notNull = true, defaultValue = "0")
    private int showSortType = 0;

    @Column(caption = "打开的方式", length = 40, notNull = true)
    private String target = "_self";

    //发布的格式 CMS  option = "0:html;1:markdown;2:link;3:img;4:layout;5:quote;6:电子报;10:公文;12:邮票"
    @Column(caption = "格式", notNull = true)
    private int formatType = 0;

    @Column(caption = "描述", length = 250, notNull = true)
    private String description = StringUtil.empty;

    @NotExpose
    @Column(caption = "密码", length = 200, notNull = true)
    private String password = StringUtil.empty;

    //保存 {76:管理员};{75:会员} (多个用;分开) xin新颁布改为{ } 表示角色，[] 表示用户
    @Column(caption = "查看角色", length = 240, notNull = true)
    private String roleIds = StringUtil.empty;

    @Column(caption = "允许发布", length = 2, option = "0:否;1:是", notNull = true, defaultValue = "0")
    private int allowPost = 1;

    @Column(caption = "是否允许回复", length = 2, option = "0:否;1:是", notNull = true, defaultValue = "1")
    private int allowReply = 1;

    @Column(caption = "是否允许下载附件", length = 2, option = "0:否;1:是", notNull = true, defaultValue = "1")
    private int allowDownAttach = 1;

    @Column(caption = "是否允许上传附件", length = 2, option = "0:否;1:是", notNull = true)
    private int allowPostAttach = 1;

    @Column(caption = "回复数量", notNull = true, defaultValue = "0")
    private int replies = 0;

    @Column(caption = "总发布数", notNull = true, defaultValue = "0")
    private int posts = 1;

    @Column(caption = "精华数量", notNull = true, defaultValue = "0")
    private int distillates = 1;

    //1发方法方法方法1190620768admin
    //论坛等常用begin
    @Column(caption = "最后发布Id", notNull = false)
    private long lastOperateId = 0;

    @Column(caption = "最后发布标题", length = 200, notNull = false)
    private String lastSubject = StringUtil.empty;

    @Column(caption = "最后发布用户", length = 50, notNull = false)
    private String lastOperateUser = StringUtil.empty;

    @Column(caption = "最后发布用户", notNull = false)
    private long lastOperateUid = 0;
    //论坛等常用end

    @Column(caption = "样式", length = 50, notNull = false)
    private String styleName = StringUtil.empty;

    @Column(caption = "节点类型", option = "0:不显示;1:首页显示;2:专题", notNull = true)
    private int nodeType = 1;

    @Column(caption = "图片URL", length = 200, notNull = true)
    private String showImage = StringUtil.empty;

    @Column(caption = "正文连接地址", length = 200, notNull = true)
    private String linkPage = StringUtil.empty;

    @Column(caption = "显示模版", length = 200, notNull = true)
    private String templatePage = StringUtil.empty;

    @Column(caption = "栏目连接地址", length = 200, notNull = true)
    private String nodeLinkPage = StringUtil.empty;

    @Column(caption = "列表模版", length = 200, notNull = true)
    private String templateListPage = StringUtil.empty;

    //CMS里边判断是否静态化，各个软件作用不同
    @Column(caption = "模版类型",notNull = true)
    private int templateType = 0;

    //做点菜时候的物品单位
    @Column(caption = "单位", length = 250, notNull = true)
    private String units = StringUtil.empty;

    @Column(caption = "栏目广告", length = 250, notNull = true)
    private String drumbeating = StringUtil.empty;

    //格式[id:name]多个使用;分开
    @Column(caption = "管理员", length = 250, notNull = false)
    private String manager = StringUtil.empty;

    @Column(caption = "文书", length = 250, notNull = false)
    private String paperwork = StringUtil.empty;

    @Column(caption = "排序时间", notNull = true)
    private Date sortDate = new Date();

    //论坛做最后发布时间
    @NotExpose
    @Column(caption = "操作时间", notNull = true)
    private Date lastDate = new Date();

    @NotExpose
    @Column(caption = "命名空间", length = 50, notNull = true)
    private String namespace = StringUtil.empty;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public String getItemValue() {
        return itemValue;
    }

    public void setItemValue(String itemValue) {
        this.itemValue = itemValue;
    }

    public String getParentNodeId() {
        return parentNodeId;
    }

    public void setParentNodeId(String parentNodeId) {
        this.parentNodeId = parentNodeId;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getNodeAction() {
        return nodeAction;
    }

    public void setNodeAction(String nodeAction) {
        this.nodeAction = nodeAction;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getOpenIcon() {
        return openIcon;
    }

    public void setOpenIcon(String openIcon) {
        this.openIcon = openIcon;
    }

    public int getHide() {
        return hide;
    }

    public void setHide(int hide) {
        this.hide = hide;
    }

    public int getSortType() {
        return sortType;
    }

    public void setSortType(int sortType) {
        this.sortType = sortType;
    }

    public int getShowSortType() {
        return showSortType;
    }

    public void setShowSortType(int showSortType) {
        this.showSortType = showSortType;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }


    /**
     * 现在已经改为 支持角色，和用户两种形式 [1:xxx][2:用户];{2:角色};
     * 和浏览权限不同,这里如果不设置，表示不能管理
     * @param roleId
     * @param uid
     * @return
     */
    public boolean isInManager(int roleId,long uid) {
        if (StringUtil.isNULL(manager)) return false;
        if ((manager.contains("[") && manager.contains("]"))|| (manager.contains("{") && manager.contains("}"))) {
            return roleIds.contains("[" + uid + StringUtil.COLON)||manager.contains("{" + roleId + StringUtil.COLON);
        }
        return StringUtil.isNULL(manager);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isInRoleIds(int roleId,long uid) {
        if (StringUtil.isNULL(roleIds)||("*".equals(roleIds))) return true;
        if ((roleIds.contains("[") && roleIds.contains("]"))|| (roleIds.contains("{") && roleIds.contains("}"))) {
            return roleIds.contains("[" + uid + StringUtil.COLON)||roleIds.contains("{" + roleId + StringUtil.COLON);
        }
        return StringUtil.isNULL(roleIds);
    }

    public String getRoleIds() {
        return roleIds;
    }


    public void setRoleIds(String roleIds) {
        this.roleIds = roleIds;
    }

    //合并
    public void joinRoleIds(String roleIds) {
        String[] lines = StringUtil.split(roleIds, StringUtil.SEMICOLON);
        for (String line:lines)
        {
            if (!this.roleIds.contains(line))
                if (StringUtil.isNULL(this.roleIds)) this.roleIds = line;
                else
                    this.roleIds = this.roleIds + StringUtil.SEMICOLON + line;
        }
    }

    public String getRolesCaption() {
        StringBuilder sb = new StringBuilder();
        String[] lines = StringUtil.split(roleIds, StringUtil.SEMICOLON);
        for (String line:lines)
        {
            sb.append(StringUtil.substringBetween(line,":","}")).append(StringUtil.SEMICOLON);
        }
        if (sb.toString().endsWith(StringUtil.SEMICOLON)) sb.setLength(sb.length()-1);
        return sb.toString();
    }

    public boolean isInPaperwork(int userId) {
        if (StringUtil.isNULL(paperwork)) return false;
        if (paperwork.contains("[") && paperwork.contains("]")) {
            return paperwork.contains("[" + userId + ":");
        }
        return StringUtil.isNULL(paperwork) || ArrayUtil.inArray(StringUtil.split(paperwork, StringUtil.SEMICOLON), userId + "", true);
    }

    public String getPaperwork() {
        return paperwork;
    }

    public void setPaperwork(String paperwork) {
        this.paperwork = paperwork;
    }

    public int getNodeType() {
        return nodeType;
    }

    public void setNodeType(int nodeType) {
        this.nodeType = nodeType;
    }

    public String getLinkPage() {
        return linkPage;
    }

    public void setLinkPage(String linkPage) {
        this.linkPage = linkPage;
    }

    public String getDrumbeating() {
        return drumbeating;
    }

    public void setDrumbeating(String drumbeating) {
        this.drumbeating = drumbeating;
    }

    public Date getSortDate() {
        return sortDate;
    }

    public void setSortDate(Date sortDate) {
        this.sortDate = sortDate;
    }

    public Date getLastDate() {
        return lastDate;
    }

    public void setLastDate(Date lastDate) {
        this.lastDate = lastDate;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getShowImage() {
        return showImage;
    }

    public void setShowImage(String showImage) {
        this.showImage = showImage;
    }

    public int getPosts() {
        return posts;
    }

    public void setPosts(int posts) {
        this.posts = posts;
    }

    public int getDistillates() {
        return distillates;
    }

    public void setDistillates(int distillates) {
        this.distillates = distillates;
    }

    public String getLastSubject() {
        return lastSubject;
    }

    public void setLastSubject(String lastSubject) {
        this.lastSubject = lastSubject;
    }

    public int getAllowPost() {
        return allowPost;
    }

    public void setAllowPost(int allowPost) {
        this.allowPost = allowPost;
    }

    public int getAllowReply() {
        return allowReply;
    }

    public void setAllowReply(int allowReply) {
        this.allowReply = allowReply;
    }

    public int getAllowDownAttach() {
        return allowDownAttach;
    }

    public void setAllowDownAttach(int allowDownAttach) {
        this.allowDownAttach = allowDownAttach;
    }

    public int getAllowPostAttach() {
        return allowPostAttach;
    }

    public void setAllowPostAttach(int allowPostAttach) {
        this.allowPostAttach = allowPostAttach;
    }


    public String getLastOperateUser() {
        return lastOperateUser;
    }

    public void setLastOperateUser(String lastOperateUser) {
        this.lastOperateUser = lastOperateUser;
    }

    public long getLastOperateUid() {
        return lastOperateUid;
    }

    public void setLastOperateUid(long lastOperateUid) {
        this.lastOperateUid = lastOperateUid;
    }

    public int getReplies() {
        return replies;
    }

    public void setReplies(int replies) {
        this.replies = replies;
    }

    public long getLastOperateId() {
        return lastOperateId;
    }

    public void setLastOperateId(long lastOperateId) {
        this.lastOperateId = lastOperateId;
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

    public boolean lookThrough(String roleId) {
        return ArrayUtil.inArray(StringUtil.split(roleId, StringUtil.SEMICOLON), roleId, false);
    }

    public String getTemplatePage() {
        return templatePage;
    }

    public void setTemplatePage(String templatePage) {
        this.templatePage = templatePage;
    }

    public String getTemplateListPage() {
        return templateListPage;
    }

    public void setTemplateListPage(String templateListPage) {
        this.templateListPage = templateListPage;
    }

    public String getInputType() {
        return inputType;
    }

    public void setInputType(String inputType) {
        this.inputType = inputType;
    }

    public String getSpelling() {
        return spelling;
    }

    public void setSpelling(String spelling) {
        this.spelling = spelling;
    }

    public String toXmlString(boolean selected) {
        StringBuilder result = new StringBuilder();
        result.append("<node id=\"").append(nodeId).append("\" text=\"").append(StringUtil.replace(caption, "'", "\\'")).append("\" ");
        result.append("openIcon=\"").append(openIcon).append("\" ");
        result.append("hide=\"").append(hide).append("\" ");
        result.append("inputType=\"").append(inputType).append("\" ");
        result.append("pid=\"").append(parentNodeId).append("\" ");
        result.append("value=\"").append(itemValue).append("\" ");
        result.append("state=\"").append(selected).append("\" ");
        result.append("sort=\"").append(sortType).append("\" />");
        return result.toString();
    }

    public int getFormatType() {
        return formatType;
    }

    public void setFormatType(int formatType) {
        this.formatType = formatType;
    }

    public int getTemplateType() {
        return templateType;
    }

    public void setTemplateType(int templateType) {
        this.templateType = templateType;
    }

    public String getNodeLinkPage() {
        return nodeLinkPage;
    }

    public void setNodeLinkPage(String nodeLinkPage) {
        this.nodeLinkPage = nodeLinkPage;
    }
}